<?php
require_once dirname(dirname(__FILE__)) . '/my/conf/config.php';

ini_set('display_errors', 'off');
error_reporting(E_ALL);

$m			= isset($_REQUEST['m']) ? $_REQUEST['m'] : 'index';
$a 			= isset($_REQUEST['a']) ? $_REQUEST['a'] : 'index';
$request	= $_REQUEST;
$url		= parse_url($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$wid		= 4;
$domain		= 'www.365inf.com';
$attr		= base::init('website')->getConfig($domain);
$core		= new core;
$template	= $core->template();
$columns	= base::init('column')->fetchAll(array('wid'=>$wid));
$cache		= new cache;

extract($request);

if (!isset($action)) errorHandle::notFound();

ob_start();
switch ($action)
{
	case 'index':
		$cache->leftTime	= 3*60;
		$cache->realpath	= ROOT . $attr['arcdir'] . '/' . 'index' . $attr['indextype'];
		
		if (!$cache->isCached())
		{
			base::init('system')->make(array('index'),$wid,'',	'', 'preview',0);
			$content	= ob_get_contents();
			$cache->setCache($content);
		}
		else print_r($cache->getCache());
		break;
		
	case 'column':
		$column	= fetchColumn($classpath);
		$cache->leftTime	= 3*60;
		$cache->realpath	= ROOT . $attr['arcdir'] . '/' . $column['classpath'] . '/' . (isset($page) ? "index_$page.html" : 'index.html');
		$page	= isset($page) ? $page : 1;
		
		if (!$cache->isCached())
		{
			base::init('system')->make(array('column'),$wid,array($column['classid']),	'', 'preview',0, $page);
			$content	= ob_get_contents();
			$cache->setCache($content);
		}
		else print_r($cache->getCache());
		break;
		
	case 'page':
		$column	= fetchColumn($classpath);
		$cache->leftTime	= 3*60;
		$cache->realpath	= ROOT . $attr['arcdir'] . '/' . $column['classpath'] . '/' . "$id.html";
		
		
		if (!$cache->isCached())
		{
			base::init('system')->make(array('page'),$wid,array($column['classid']),	$id, 'preview',0);
			$content	= ob_get_contents();
			$cache->setCache($content);
		}
		else print_r($cache->getCache());
		break;
		
	default:break;
}
class cache
{
	public $realpath;
	public $leftTime;
	
	public function isCached()
	{
		#return false;
		if (is_file($this->realpath)
			&& (time() - filectime($this->realpath)) < $this->leftTime
			&& filesize($this->realpath) > 1024)
			return true;
		else return false;
	}
	
	public function setCache($content)
	{
		$f	= fopen($this->realpath,'w');
		fwrite($f, $content);
		fclose($f);
	}
	
	public function getCache()
	{
		return file_get_contents($this->realpath);
	}
}
function fetchColumn($classpath)
{
	global $columns;
	foreach ($columns as $column)
	{
		if ($classpath == $column['classpath']) return $column;
	}
}